﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;

namespace FirstWpfApp
{
    /// <summary>
    /// Interaction logic for StaffManagementPage.xaml
    /// </summary>
    public partial class StaffManagementPage : Window
    {
        BindingList<Staff> StaffList = new BindingList<Staff>();
        public StaffManagementPage()
        {
            InitializeComponent();
            //this.DataContext = this;
            //getData();

			// Please care code as follow. you are used different object as data of table. 
			// you should used same object as data, then you can operate it anywhere.
			taStaff.Fill(StaffData.Staffs);
			this.grdStaffs.ItemsSource = StaffData.Staffs;
			this.View = (CollectionView)(CollectionViewSource.GetDefaultView(this.StaffData.Staffs));
        }



        private StaffDataSet StaffData = new StaffDataSet();
        private StaffDataSetTableAdapters.StaffsTableAdapter taStaff = new StaffDataSetTableAdapters.StaffsTableAdapter();
        private StaffDataSetTableAdapters.TableAdapterManager taManager = new StaffDataSetTableAdapters.TableAdapterManager();
        private StaffDataSet.StaffsDataTable staffTable = new StaffDataSet.StaffsDataTable();
        private CollectionView View;

        //void getData()
        //{
        //    for (int i = 0; i < 4; i++)
        //    {
        //        StaffList.Add(new Employee("Name" + i.ToString(), i));
        //    }

        //}

        void clearData()
        {
            txtName.Text = string.Empty;
            txtAge.Text = string.Empty;
            cmbStaffType.SelectedIndex = 0;
        }

        private void Create(object sender, RoutedEventArgs e)
        {

            string name = txtName.Text;
            int age = int.Parse(txtAge.Text);
            Staff newStaff = null;
            switch ((this.cmbStaffType.SelectedItem as ComboBoxItem).Content.ToString())
            {
                case "Employee":
                    newStaff = new Employee(name, age);
                    break;
                case "Contractor":
                    newStaff = new Contractor(name, age);
                    break;
                case "Intern":
                    newStaff = new Intern(name, age);
                    break;
            }

            if (MessageBoxResult.OK == MessageBox.Show("You request to add a new item",
                "Are you sure?",
                MessageBoxButton.OKCancel))

            {
                StaffDataSet.StaffsRow row = this.StaffData.Staffs.NewStaffsRow();
				//row.Id = 1;
				row.Id = 0;
				row.Name = txtName.Text;
				row.Age = int.Parse(txtAge.Text);
				//as follow code, it is hardcode. 
				//TODO: should change to follow user's chose.
				row.Staff_Type = "Employee";
				this.StaffData.Staffs.AddStaffsRow(row);
				this.View.MoveCurrentToLast();
                //StaffList.Add(newStaff);
				
                //clearData();
            }
        }

        private void Update(object sender, RoutedEventArgs e)
        {
            string updateName = txtName.Text;
            int updateAge = int.Parse(txtAge.Text);

            if (MessageBoxResult.OK == MessageBox.Show("You request to update this item",
                "Are you sure?",
                MessageBoxButton.OKCancel))
            {
                (grdStaffs.SelectedItem as DataRowView)["Name"] = updateName;
                (grdStaffs.SelectedItem as DataRowView)["Age"] = updateAge;
            }
        }

        private void Delete(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.OK == MessageBox.Show("Are you sure?",
            "Delete Files",
            MessageBoxButton.OKCancel))
            {
                StaffList.Remove((grdStaffs.SelectedItem as Staff));
                clearData();
            }
        }


        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (grdStaffs.SelectedItem == null || grdStaffs.SelectedItem.ToString() == "{NewItemPlaceholder}") { return; }
            txtName.Text = (grdStaffs.SelectedItem as DataRowView)["Name"].ToString();
            txtAge.Text = (grdStaffs.SelectedItem as DataRowView)["Age"].ToString();

            switch ((grdStaffs.SelectedItem as DataRowView)["Staff_Type"].ToString())
            {
                case "Employee":
                    cmbStaffType.SelectedIndex = 0;
                    break;
                case "Contractor":
                    cmbStaffType.SelectedIndex = 1;
                    break;
                case "Intern":
                    cmbStaffType.SelectedIndex = 2;
                    break;
            }
        }
    }
}
